
clear
do "...\First.do"
********************************************************************************
use "$work\analysis_sample.dta"
keep if inrange(age,40,70)

g help=1 if inrange(timing,-4,0)
bys pnr: egen N_pre_2=total(help)
tab N_pre_2
keep if N_pre_2==5

drop if timing>3
drop if timing<-3



********************************************************************************
*** DDD
********************************************************************************
cap drop interaction2 interaction3 ddd3
g interaction2=low_ses*help3
g interaction3=low_ses*post
g ddd3=low_ses*help3*post


********************************************************************************
* Specification table - Table A9
********************************************************************************

* Main results
sum death if post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001) 
local sd=round(`r(sd)'*1000/1000, 0.00001) 

sum death if post==1 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help3==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

areg death help3 low_ses interaction2 if post==1, a(year) cluster(newgpyr)
local beta=_b[interaction2]
local effect=round(`beta'/`mean0'*100, 0.01)
disp `effect'


outreg2 using "$table\TableA9.doc", replace tex label keep(interaction2 help3 low_ses) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0',  Effect in \%, `effect', Patient Characteristics , Y , Old GP FE , Y , Patient ID FE , N , Old x new GP FE , N)



sum death if post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if post==1 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help3==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

areg death help3 low_ses interaction2 $patient_X if post==1, a(year) cluster(newgpyr)

outreg2 using "$table\TableA9.doc", append tex label keep(interaction2 help3 low_ses) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0',  Effect in \%, `effect', Patient Characteristics , Y , Old GP FE , Y , Patient ID FE , N , Old x new GP FE , N)


**
sum death if post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if post==1 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 


ttest death if post==1 & help3==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help3 low_ses interaction2 $patient_X if post==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableA9.doc", append tex label keep(interaction2 help3 low_ses) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0',  Effect in \%, `effect', Patient Characteristics , Y , Old GP FE , Y , Patient ID FE , N , Old x new GP FE , N)

***
sum death if post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if post==1 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help3==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableA9.doc", append tex label keep(interaction2 help3 low_ses) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0',  Effect in \%, `effect', Patient Characteristics , Y , Old GP FE , Y , Patient ID FE , N , Old x new GP FE , N)


**
sum death if post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if post==1 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 


ttest death if post==1 & help3==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help3 low_ses interaction2 $patient_X if post==1, a(old_new_gp year) cluster(newgpyr)

outreg2 using "$table\TableA9.doc", append tex label keep(interaction2 help3 low_ses) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0',  Effect in \%, `effect', Patient Characteristics , Y , Old GP FE , Y , Patient ID FE , N , Old x new GP FE , N) 
 

*******************************************************************************
** Cause of death
* table 3
********************************************************************************

sum death if post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if post==1 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help3==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)


reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\Table3.doc", replace tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') 


foreach var of varlist death_CVC death_cancer death_cancer_lung  {

sum `var' if post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum `var' if post==1 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest  `var' if post==1 & help3==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe `var' help3 low_ses interaction2 $patient_X $gp_X if post==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\Table3.doc", append  tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') 



}


********************************************************************************
* Table D4
********************************************************************************


** Female
sum death if male==0 &  post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if post==1 & low_ses==1 & male==0
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help3==0 & male==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)


reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1 & male==0, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD4_PanelA.doc", replace tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0',  Effect in \%, `effect') 


foreach var of varlist death_CVC death_cancer death_cancer_lung {

sum `var' if  male==0 &  post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum `var' if post==1 & low_ses==1 & male==0
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest  `var' if post==1 & help3==0 & timing<4 & male==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe `var'  help3 low_ses interaction2 $patient_X $gp_X if post==1 & male==0, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD4_PanelA.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0',  Effect in \%, `effect') 

}

** Male
sum death if male==1 &  post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if post==1 & low_ses==1 & male==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help3==0 & male==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)


reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1 & male==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD4_PanelB.doc", replace  tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') 




foreach var of varlist death_CVC death_cancer death_cancer_lung  {

sum `var' if  male==1  &  post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum `var' if post==1 & low_ses==1 & male==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest  `var' if post==1 & help3==0 & timing<4 & male==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe `var'  help3 low_ses interaction2 $patient_X $gp_X if post==1 & male==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD4_PanelB.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0',  Effect in \%, `effect') 
}





*** AGE

sum death if  age>=55 &  post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if post==1 & low_ses==1 & age>=55
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help3==0 &  age>=55, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1 &  age>=55, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD4_PanelC.doc", replace tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') 



foreach var of varlist death_CVC death_cancer death_cancer_lung {

sum `var' if  age>=55 &  post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum `var' if post==1 & low_ses==1 & age>=55
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest  `var' if post==1 & help3==0 &  age>=55, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe `var' help3 low_ses interaction2 $patient_X $gp_X if post==1 &  age>=55, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD4_PanelC.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') 

}


sum death if age<55 &  post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if post==1 & low_ses==1 & age<55
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help3==0 & age<55, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1 &  age<55, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD4_PanelD.doc", replace tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') 

foreach var of varlist death_CVC death_cancer death_cancer_lung {

sum `var' if age<55 &  post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum `var' if post==1 & low_ses==1 & age<55
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest  `var' if post==1 & help3==0 & age<55, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe `var' help3 low_ses interaction2 $patient_X $gp_X if post==1 &  age<55, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD4_PanelD.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') 

}




********************************************************************************
* CARE
********************************************************************************

* Translate to US dollars
replace total_cost_GP=total_cost_GP/7.5


sum GP_visit if post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum GP_visit if post==0 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest GP_visit if post==1 & help3==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe GP_visit post help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X, a(id year) cluster(id)


outreg2 using "$table\TableD5.doc", replace tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') 

foreach var of varlist N_visits2 services total_cost_GP   {

sum `var' if post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum `var' if post==0 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest  `var' if post==1 & help3==0 , by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe `var' post help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X, a(id year) cluster(id)

outreg2 using "$table\TableD5.doc", append tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') 
}



